import { createStore } from 'vuex'

export default createStore({
  state: {
    basket: {
      select: [
        { id: 1, title: '正品阿迪达斯 Adidas x Parley Ultra Boost DNA联名线条纹跑步鞋', img: '//gw.alicdn.com/bao/uploaded/i4/501803377/O1CN01skMKyA1aoi7bloqHO_!!501803377-0-lubanu-s.jpg_500x500q90.jpg_.webp', price: 66, num: { num: 1, max: 1000 }, size: 'xxl', select: false },
        { id: 1, title: '正品阿迪达斯 Adidas x Parley Ultra Boost DNA联名线条纹跑步鞋', img: '//gw.alicdn.com/bao/uploaded/i4/501803377/O1CN01skMKyA1aoi7bloqHO_!!501803377-0-lubanu-s.jpg_500x500q90.jpg_.webp', price: 66, num: { num: 1, max: 1000 }, size: 'xxl', select: false },
        { id: 1, title: '正品阿迪达斯 Adidas x Parley Ultra Boost DNA联名线条纹跑步鞋', img: '//gw.alicdn.com/bao/uploaded/i4/501803377/O1CN01skMKyA1aoi7bloqHO_!!501803377-0-lubanu-s.jpg_500x500q90.jpg_.webp', price: 66, num: { num: 1, max: 1000 }, size: 'xxl', select: false },
        { id: 1, title: '正品阿迪达斯 Adidas x Parley Ultra Boost DNA联名线条纹跑步鞋', img: '//gw.alicdn.com/bao/uploaded/i4/501803377/O1CN01skMKyA1aoi7bloqHO_!!501803377-0-lubanu-s.jpg_500x500q90.jpg_.webp', price: 66, num: { num: 1, max: 1000 }, size: 'xxl', select: false },
        { id: 1, title: '正品阿迪达斯 Adidas x Parley Ultra Boost DNA联名线条纹跑步鞋', img: '//gw.alicdn.com/bao/uploaded/i4/501803377/O1CN01skMKyA1aoi7bloqHO_!!501803377-0-lubanu-s.jpg_500x500q90.jpg_.webp', price: 66, num: { num: 1, max: 1000 }, size: 'xxl', select: false },
        { id: 1, title: '正品阿迪达斯 Adidas x Parley Ultra Boost DNA联名线条纹跑步鞋', img: '//gw.alicdn.com/bao/uploaded/i4/501803377/O1CN01skMKyA1aoi7bloqHO_!!501803377-0-lubanu-s.jpg_500x500q90.jpg_.webp', price: 66, num: { num: 1, max: 1000 }, size: 'xxl', select: false },
        { id: 1, title: '正品阿迪达斯 Adidas x Parley Ultra Boost DNA联名线条纹跑步鞋', img: '//gw.alicdn.com/bao/uploaded/i4/501803377/O1CN01skMKyA1aoi7bloqHO_!!501803377-0-lubanu-s.jpg_500x500q90.jpg_.webp', price: 66, num: { num: 1, max: 1000 }, size: 'xxl', select: false },
        { id: 1, title: '正品阿迪达斯 Adidas x Parley Ultra Boost DNA联名线条纹跑步鞋', img: '//gw.alicdn.com/bao/uploaded/i4/501803377/O1CN01skMKyA1aoi7bloqHO_!!501803377-0-lubanu-s.jpg_500x500q90.jpg_.webp', price: 66, num: { num: 1, max: 1000 }, size: 'xxl', select: false },
        { id: 1, title: '正品阿迪达斯 Adidas x Parley Ultra Boost DNA联名线条纹跑步鞋', img: '//gw.alicdn.com/bao/uploaded/i4/501803377/O1CN01skMKyA1aoi7bloqHO_!!501803377-0-lubanu-s.jpg_500x500q90.jpg_.webp', price: 66, num: { num: 1, max: 1000 }, size: 'xxl', select: false },
        { id: 1, title: '正品阿迪达斯 Adidas x Parley Ultra Boost DNA联名线条纹跑步鞋', img: '//gw.alicdn.com/bao/uploaded/i4/501803377/O1CN01skMKyA1aoi7bloqHO_!!501803377-0-lubanu-s.jpg_500x500q90.jpg_.webp', price: 66, num: { num: 1, max: 1000 }, size: 'xxl', select: false }
      ],
      selectAll: false,
      amount: 0,
      selectNum: 0
    },
    mainHeight: 0
  },
  mutations: {
    /*
      mutations不能执行异步操作
      只有mutations中定义的函数才能修改state中的值
    */
    editMainHeight(state, step) {
      state.mainHeight = step
      console.log(state.mainHeight, step)
    },
    selectAll(state, step) {
      state.basket.selectAll = step
      state.basket.select.forEach((v, k) => {
        v.select = step
      })
    },
    editBasketAmount(state) {
      console.log('editBasketAmount')
      state.basket.amount = 0
      state.basket.selectNum = 0
      state.basket.select.forEach((v, k) => {
        if (v.select) {
          state.basket.selectNum++
          state.basket.amount += v.price * v.num.num
        }
      })
    },
    filterBasketSelect(state, step) {
      state.basket.select = step
    }
  },
  actions: {
    /* actions中不能直接修改state中的数据
       只能通过 context.commit() 触发某个mutation
    */
  },
  modules: {
  },
  getters: {
    /* getter是对store中的数据进行加工处理形成新数据 */
    getBasketSelect: state => {
      return state.select
    }
  }
})
